

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.net.asyn.msgcenter &mdash; Cup 3.2.19 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 

  
  <script src="../../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../../index.html" class="icon icon-home"> Cup
          

          
          </a>

          
            
            
              <div class="version">
                3.2.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../cup.html">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">Cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
          <li><a href="../../net.html">cup.net</a> &raquo;</li>
        
      <li>cup.net.asyn.msgcenter</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.net.asyn.msgcenter</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:descrition:</span>
<span class="sd">    msg center related module</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">abc</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">socket</span>
<span class="kn">import</span> <span class="nn">threading</span>

<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">log</span>
<span class="kn">from</span> <span class="nn">cup.net.asyn</span> <span class="k">import</span> <span class="n">conn</span>
<span class="kn">from</span> <span class="nn">cup.net.asyn</span> <span class="k">import</span> <span class="n">msg</span> <span class="k">as</span> <span class="n">async_msg</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;IMessageCenter&#39;</span><span class="p">]</span>

<span class="c1"># CHECK_OFF=0</span>
<span class="c1"># CHECK_ON=1</span>


<div class="viewcode-block" id="IMessageCenter"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.msgcenter.IMessageCenter">[docs]</a><span class="k">class</span> <span class="nc">IMessageCenter</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Message center class</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">thdpool_param</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stat_intvl</span><span class="o">=</span><span class="mi">20</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">thdpool_param</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">thdpool_param</span> <span class="o">=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_conn_mgr</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">CConnectionManager</span><span class="p">(</span>
            <span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">thdpool_param</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stop</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stat_intvl</span> <span class="o">=</span> <span class="n">stat_intvl</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stat_cond</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Condition</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_type_man</span> <span class="o">=</span> <span class="n">async_msg</span><span class="o">.</span><span class="n">CMsgType</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_type_man</span><span class="o">.</span><span class="n">register_types</span><span class="p">(</span><span class="n">async_msg</span><span class="o">.</span><span class="n">MSG_TYPE2NUM</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_bind_port</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;bind port for message center&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_conn_mgr</span><span class="o">.</span><span class="n">bind</span><span class="p">()</span>

<div class="viewcode-block" id="IMessageCenter.global_sock_keepalive"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.msgcenter.IMessageCenter.global_sock_keepalive">[docs]</a>    <span class="k">def</span> <span class="nf">global_sock_keepalive</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
        <span class="n">after_idle_sec</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">max_fails</span><span class="o">=</span><span class="mi">5</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set TCP keepalive on an open socket.</span>
<span class="sd">        It activates after 1 second (after_idle_sec) of idleness,</span>
<span class="sd">        then sends a keepalive ping once every 3 seconds (interval_sec),</span>
<span class="sd">        and closes the connection after 5 failed ping (max_fails), or 15 sec</span>

<span class="sd">        Notice, this will set all sockets this way.</span>

<span class="sd">        :param sock:</span>
<span class="sd">            socket</span>
<span class="sd">        :param after_idle_sec:</span>
<span class="sd">            for TCP_KEEPIDLE. May not work, depends on ur system</span>
<span class="sd">        :param interval_sec:</span>
<span class="sd">            for TCP_KEEPINTVL</span>
<span class="sd">        :param max_fails:</span>
<span class="sd">            for TCP_KEEPCNT</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_conn_mgr</span><span class="o">.</span><span class="n">global_sock_keepalive</span><span class="p">(</span>
            <span class="n">after_idle_sec</span><span class="p">,</span> <span class="n">interval_sec</span><span class="p">,</span> <span class="n">max_fails</span>
        <span class="p">)</span></div>

<div class="viewcode-block" id="IMessageCenter.setup"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.msgcenter.IMessageCenter.setup">[docs]</a>    <span class="k">def</span> <span class="nf">setup</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        setup the message center</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_bind_port</span><span class="p">()</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">except</span> <span class="n">socket</span><span class="o">.</span><span class="n">error</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;bind error:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">error</span><span class="p">))</span>
            <span class="k">return</span> <span class="kc">False</span></div>

<div class="viewcode-block" id="IMessageCenter.dump_stat"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.msgcenter.IMessageCenter.dump_stat">[docs]</a>    <span class="k">def</span> <span class="nf">dump_stat</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        dump message center class</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;mysql dump_stat service started&#39;</span><span class="p">)</span>
        <span class="k">while</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stop</span><span class="p">:</span>
            <span class="n">ind</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">while</span> <span class="n">ind</span> <span class="o">&lt;</span> <span class="mi">30</span><span class="p">:</span>
                <span class="n">ind</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stop</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;msgcenter dump_stat service stopped&#39;</span><span class="p">)</span>
                    <span class="k">return</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_stat_cond</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_conn_mgr</span><span class="o">.</span><span class="n">dump_stats</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_stat_cond</span><span class="o">.</span><span class="n">wait</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_stat_intvl</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_stat_cond</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;msgcenter dump_stat service stopped&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="IMessageCenter.post_msg"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.msgcenter.IMessageCenter.post_msg">[docs]</a>    <span class="k">def</span> <span class="nf">post_msg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        post a net msg</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_conn_mgr</span><span class="o">.</span><span class="n">push_msg2sendqueue</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span></div>

<div class="viewcode-block" id="IMessageCenter.close_socket"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.msgcenter.IMessageCenter.close_socket">[docs]</a>    <span class="k">def</span> <span class="nf">close_socket</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">recv_socket</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;close the socket by msg&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_conn_mgr</span><span class="o">.</span><span class="n">close_socket</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">recv_socket</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_post_ackok_msg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to_addr</span><span class="p">,</span> <span class="n">from_addr</span><span class="p">,</span> <span class="n">uniq_id</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        create an ack msg</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;post ack ok msg.&#39;</span><span class="p">)</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="n">async_msg</span><span class="o">.</span><span class="n">CNetMsg</span><span class="p">(</span><span class="n">is_postmsg</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">msg</span><span class="o">.</span><span class="n">set_to_addr</span><span class="p">(</span><span class="n">to_addr</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">to_addr</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="n">msg</span><span class="o">.</span><span class="n">set_from_addr</span><span class="p">(</span><span class="n">from_addr</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">from_addr</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="n">msg</span><span class="o">.</span><span class="n">set_msg_type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_type_man</span><span class="o">.</span><span class="n">getnumber_bytype</span><span class="p">(</span><span class="s1">&#39;ACK_OK&#39;</span><span class="p">))</span>
        <span class="n">msg</span><span class="o">.</span><span class="n">set_flag</span><span class="p">(</span><span class="n">async_msg</span><span class="o">.</span><span class="n">MSG_FLAG2NUM</span><span class="p">[</span><span class="s1">&#39;FLAG_NORMAL&#39;</span><span class="p">])</span>
        <span class="n">msg</span><span class="o">.</span><span class="n">set_uniq_id</span><span class="p">(</span><span class="n">uniq_id</span><span class="p">)</span>
        <span class="n">msg</span><span class="o">.</span><span class="n">set_body</span><span class="p">(</span><span class="s1">&#39;0&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">post_msg</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>

<div class="viewcode-block" id="IMessageCenter.pre_handle"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.msgcenter.IMessageCenter.pre_handle">[docs]</a>    <span class="k">def</span> <span class="nf">pre_handle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">function</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;pre_handle. Internal use ONLY. Do NOT call it directly.&quot;&quot;&quot;</span>
        <span class="n">function</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_on_recv_ackmsg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">netmsg</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;on receiving ack msg&quot;&quot;&quot;</span>

<div class="viewcode-block" id="IMessageCenter.handle"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.msgcenter.IMessageCenter.handle">[docs]</a>    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">handle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        handle function which should be implemented by</span>
<span class="sd">        sub-class.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;handle in msgcenter&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="IMessageCenter.default_handle"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.msgcenter.IMessageCenter.default_handle">[docs]</a>    <span class="k">def</span> <span class="nf">default_handle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>  <span class="c1"># pylint: disable=W0613,R0201</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        default handle for msgcenter</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">msg_ackflag</span> <span class="o">=</span> <span class="n">async_msg</span><span class="o">.</span><span class="n">MSG_FLAG2NUM</span><span class="p">[</span><span class="s1">&#39;FLAG_ACK&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">msg_ackflag</span> <span class="o">&amp;</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_flag</span><span class="p">()</span> <span class="o">==</span> <span class="n">msg_ackflag</span><span class="p">:</span>
            <span class="c1"># no need to handle it</span>
            <span class="k">pass</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                <span class="s1">&#39;got a msg that you cannot hanlde, default will skip it. &#39;</span>
                <span class="s1">&#39;msg received, type:</span><span class="si">%d</span><span class="s1">, flag:</span><span class="si">%d</span><span class="s1">, from:</span><span class="si">%s</span><span class="s1">, uniqid:</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span>
                <span class="p">(</span>
                    <span class="n">msg</span><span class="o">.</span><span class="n">get_msg_type</span><span class="p">(),</span>
                    <span class="n">msg</span><span class="o">.</span><span class="n">get_flag</span><span class="p">(),</span>
                    <span class="nb">str</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">get_from_addr</span><span class="p">()),</span>
                    <span class="n">msg</span><span class="o">.</span><span class="n">get_uniq_id</span><span class="p">()</span>
                <span class="p">)</span>
            <span class="p">)</span>
            <span class="k">del</span> <span class="n">msg</span></div>

    <span class="k">def</span> <span class="nf">_run_conn_manager</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        run conn manager</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;run conn manager poll&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_conn_mgr</span><span class="o">.</span><span class="n">poll</span><span class="p">()</span>

<div class="viewcode-block" id="IMessageCenter.is_stopping"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.msgcenter.IMessageCenter.is_stopping">[docs]</a>    <span class="k">def</span> <span class="nf">is_stopping</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        is msg center being stopped</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stop</span></div>

<div class="viewcode-block" id="IMessageCenter.stop"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.msgcenter.IMessageCenter.stop">[docs]</a>    <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">force_stop</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        stop the message center</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;To stop the msgcenter&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_conn_mgr</span><span class="o">.</span><span class="n">stop</span><span class="p">(</span><span class="n">force_stop</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stop</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stat_cond</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stat_cond</span><span class="o">.</span><span class="n">notify</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stat_cond</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;msgcenter stopped&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="IMessageCenter.run"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.msgcenter.IMessageCenter.run">[docs]</a>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        run the msgcenter</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">setup</span><span class="p">():</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="n">thd_conn_man</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_run_conn_manager</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">())</span>
        <span class="n">thd_conn_man</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        <span class="n">thd_stat</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dump_stat</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">())</span>
        <span class="n">thd_stat</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        <span class="n">ind</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">msg_ackflag</span> <span class="o">=</span> <span class="n">async_msg</span><span class="o">.</span><span class="n">MSG_FLAG2NUM</span><span class="p">[</span><span class="s1">&#39;FLAG_ACK&#39;</span><span class="p">]</span>
        <span class="k">while</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stop</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_conn_mgr</span><span class="o">.</span><span class="n">get_recv_msg</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">ind</span> <span class="o">&gt;=</span> <span class="mi">10000</span><span class="p">:</span>
                <span class="n">recv_queue</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_conn_mgr</span><span class="o">.</span><span class="n">get_recv_queue</span><span class="p">()</span>
                <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;msgcenter netmsg queue size:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">recv_queue</span><span class="o">.</span><span class="n">qsize</span><span class="p">()))</span>
                <span class="n">ind</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">if</span> <span class="n">msg</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                        <span class="s1">&#39;msg received, type:</span><span class="si">%d</span><span class="s1">, flag:</span><span class="si">%d</span><span class="s1">, from:</span><span class="si">%s</span><span class="s1">, uniqid:</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span>
                        <span class="p">(</span>
                            <span class="n">msg</span><span class="o">.</span><span class="n">get_msg_type</span><span class="p">(),</span>
                            <span class="n">msg</span><span class="o">.</span><span class="n">get_flag</span><span class="p">(),</span>
                            <span class="nb">str</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">get_from_addr</span><span class="p">()),</span>
                            <span class="n">msg</span><span class="o">.</span><span class="n">get_uniq_id</span><span class="p">()</span>
                        <span class="p">)</span>
                    <span class="p">)</span>
                    <span class="n">ind</span> <span class="o">+=</span> <span class="mi">1</span>
                    <span class="k">if</span> <span class="n">msg_ackflag</span> <span class="o">&amp;</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_flag</span><span class="p">()</span> <span class="o">==</span> <span class="n">msg_ackflag</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_conn_mgr</span><span class="o">.</span><span class="n">push_msg2needack_queue</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">handle</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
                <span class="c1"># pylint: disable=W0703</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                        <span class="s1">&#39;get a msg that cannot be handled.&#39;</span>
                        <span class="s1">&#39;Seems network err:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">err</span><span class="p">)</span>
                    <span class="p">)</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="kc">True</span></div></div>

<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../../',
            VERSION:'3.2.19',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../../_static/doctools.js"></script>
      <script type="text/javascript" src="../../../../_static/language_data.js"></script>

  

  <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>